package com.badibadi.mytools;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import com.badibadi.infos.AreaModel;
import com.badibadi.infos.Results;
import com.badibadi.uniclubber.R;

public class AreaDialog {
	private Context context;
	public AreaHolderView holderView;
	public List<AreaModel> areaModelscountry, areaModelssheng, areaModelscity;
	private String country = null, sheng = null, city = null, area_id = null,
			sex_temp = null;
	private int languageType = 0;
	private Handler handler;
	private Message mes;
	private Bundle bun;

	public AreaDialog(Context context, int languageType) {
		this.context = context;
		this.languageType = languageType;
		holderView = new AreaHolderView();
	}

	public AreaDialog(Context context, int languageType, Handler handler) {
		this.context = context;
		this.languageType = languageType;
		this.handler = handler;
		holderView = new AreaHolderView();
	}

	public void AreaDisplayDialog() {
		mes = new Message();
		bun = new Bundle();
		new AlertDialog.Builder(context)
				.setTitle(context.getResources().getString(R.string.wx_txt_75))// <string name="wx_txt_75">设置来自：</string>
				.setView(AreaLayout(context))
				.setNegativeButton(
						context.getResources().getString(R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (country != null && sheng != null
										&& city != null) {
									// from.setText(country + " " + sheng + " "
									// + city);
									mes.what = 7;
									bun.putString("来自", country + " " + sheng
											+ " " + city+" "+area_id);
									//    ("城市id是:" + area_id);
									mes.setData(bun);
									handler.sendMessage(mes);
								}
							}
						})
				.setPositiveButton(
						context.getResources().getString(R.string.cancel),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {

							}
						}).show();
	}

	private View AreaLayout(final Context context) {
		View view = null;
		LayoutInflater inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.area_judge_layout_main, null);
		holderView.area_country_name_spinner = (Spinner) view
				.findViewById(R.id.area_country_name_spinner);
		holderView.area_province_name_spinner = (Spinner) view
				.findViewById(R.id.area_province_name_spinner);
		holderView.area_city_name_spinner = (Spinner) view
				.findViewById(R.id.area_city_name_spinner);
		holderView.area_city_name_spinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						if (areaModelscity != null && !areaModelscity.isEmpty()) {
							city = areaModelscity.get(position).getArea_name();
							area_id = areaModelscity.get(position).getId();
						}
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						// TODO Auto-generated method stub
					}
				});
		holderView.area_province_name_spinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						if (areaModelssheng != null
								&& !areaModelssheng.isEmpty()) {
							sheng = areaModelssheng.get(position)
									.getArea_name();
//							   ("省的id:"
//									+ areaModelssheng.get(position).getId());
							Getareathread(
									areaModelssheng.get(position).getId(), "市");
						}
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						// TODO Auto-generated method stub

					}
				});
		holderView.area_country_name_spinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						if (areaModelscountry != null
								&& !areaModelscountry.isEmpty()) {
							country = areaModelscountry.get(position)
									.getArea_name();
//							   ("国家的id:"
//									+ areaModelscountry.get(position).getId());
							Getareathread(areaModelscountry.get(position)
									.getId(), "省");
						}
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
						// TODO Auto-generated method stub

					}
				});

		return view;
	}

	private void Getareathread(final String df, final String panduan) {
		Utils.showPrgress(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", df);
				map.put("languageType", TempTools.BackLanguage(languageType));
				//    ("地区的upid" + map.toString());
				String content = Utils.sendRequest(map, Constants.BadiUrl
						+ "area");
				if (content != null) {
					//    ("结果是:" + content);
					Results results = Utils.checkResult_NNN(context, content);
					if (results != null && results.getRetmsg() != null) {
						//    ("得到的结果是：" + results.getRetmsg());
						try {
							if (panduan.equals("省")) {
								areaModelssheng = JSONUtils
										.getListByJsonString(
												results.getRetmsg(),
												AreaModel.class);
								handler.sendEmptyMessage(3);
							} else if (panduan.equals("市")) {
								areaModelscity = JSONUtils.getListByJsonString(
										results.getRetmsg(), AreaModel.class);
								handler.sendEmptyMessage(4);
							}
						} catch (JSONException e) {
//							   ("解析出错");
							   
						}

					}
				} else {
					handler.sendEmptyMessage(2);
				}
			}
		}).start();
	}

	public void Getareathread() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("languageType", TempTools.BackLanguage(languageType));
				String content = Utils.sendRequest(map, Constants.BadiUrl
						+ "area");
				if (content != null) {
					//    ("结果是:" + content);
					Results results = Utils.checkResult_NNN(context, content);
					if (results != null && results.getRetmsg() != null) {
						//    ("得到的结果是：" + results.getRetmsg());
						try {
							areaModelscountry = Utils.getListByJsonString(
									results.getRetmsg(), AreaModel.class);
							handler.sendEmptyMessage(1);
						} catch (JSONException e) {
//							   ("解析出错");
							   
						}
					}
				} else {
					handler.sendEmptyMessage(2);
				}
			}
		}).start();
	}

	public static class AreaHolderView {
		public Spinner area_country_name_spinner,// 国家
				area_province_name_spinner,// 省
				area_city_name_spinner;// 市
	}
}
